from PyQt4 import QtCore, QtGui
from item import *
from mainWindow import *

class Sender(QtCore.QObject):
    def sendGameOver(self):
        self.emit(QtCore.SIGNAL('gameOver()'))

sender =Sender()

def updatePos(items):
    for item in items:
        # if item.invincible != 0:
            # item.invincible -= 1
        item.nextPos()
        # print(item.brickId, item.xPos, item.yPos)


def handleAccident(items):
    for itemA in items:
        # border event
        border = hitBorder(itemA)
        if border != 0:
            handleBorder(itemA, border)
            # collision event
        for itemB in items:
            if itemA.brickId == itemB.brickId:
                continue
            if hitBrick(itemA, itemB) and itemA.invincible == 0:
                print('hit A', itemA.brickId, itemA.xPos, itemA.yPos)
                print('hit B', itemB.brickId, itemB.xPos, itemB.yPos)
                handleCollision(itemA, itemB)
                # itemA.invincible = 20
                break


def hitBrick(brickA, brickB):
    xHit = False
    yHit = False
    if brickB.xPos + brickB.widthSize >= brickA.xPos + brickA.widthSize >= brickB.xPos:
        xHit = True  # left
    elif brickB.xPos <= brickA.xPos <= brickB.xPos + brickB.widthSize:
        xHit = True  # right
    if brickB.yPos <= brickA.yPos <= brickB.yPos + brickB.heightSize:
        yHit = True  # up
    elif brickB.yPos + brickB.heightSize >= brickA.yPos + brickA.heightSize >= brickB.yPos:
        yHit = True  # down
    if xHit and yHit:
        return True
    else:
        return False

# brick type: black:user red:rebound, yellow:attach, blue:through
def handleCollision(posBrick, negBrick):
    # distribute collision event to handler for each type, set forward for posBrick
    global sender
    if posBrick.brickType == 0:  # user
        sender.sendGameOver()
    elif posBrick.brickType == 1:  # rebound
        reboundCollision(posBrick, negBrick)
    elif posBrick.brickType == 2:  # attach
        attachCollision(posBrick, negBrick)
    elif posBrick.brickType == 3:  # through
        throughCollision(posBrick, negBrick)


def reboundCollision(posBrick, negBrick):
    if posBrick.xPos + posBrick.widthSize >= negBrick.xPos or posBrick.xPos <= negBrick.xPos + negBrick.widthSize:
        posBrick.setForward(-posBrick.xForward, posBrick.yForward)
    if posBrick.yPos + posBrick.heightSize >= negBrick.yPos or posBrick.yPos <= negBrick.yPos + negBrick.heightSize:
        posBrick.setForward(posBrick.xForward, -posBrick.yForward)


def attachCollision(posBrick, negBrick):
    reboundBorder(posBrick, negBrick)
    # posBrick.setForward(negBrick.forwardX, negBrick.forwardY)


def throughCollision(posBrick, negBrick):
    reboundBorder(posBrick, negBrick)

def hitBorder(brick):
    if brick.xPos <= 0:
        return 1  # left
    elif brick.xPos + brick.widthSize >= BricksScene.widthSize:
        return 2  # right
    elif brick.yPos <= 0:
        return 3  # up
    elif brick.yPos + brick.heightSize > BricksScene.heightSize:
        return 4  # down
    else:
        return 0


def handleBorder(brick, border):
    global sender
    if brick.brickType == 0:  # user
        sender.sendGameOver()
    elif brick.brickType == 1:  # rebound
        reboundBorder(brick, border)
    elif brick.brickType == 2:  # attach
        attachBorder(brick, border)
    elif brick.brickType == 3:  # through
        throughCollision(brick, border)


def reboundBorder(brick, border):
    throughBorder(brick, border)
    # if border == 1 or border == 2:
    #     brick.setForward(- brick.forwardX, brick.forwardY)
    # if border == 3 or border == 4:
    #     brick.setForward(brick.forwardX, - brick.forwardY)


def attachBorder(brick, border):
    throughBorder(brick, border)
    # reboundBorder(brick, border)


def throughBorder(brick, border):
    if border == 1:
        brick.xPos = BricksScene.widthSize
    elif border == 2:
        brick.xPos = 0
    elif border == 3:
        brick.yPos = BricksScene.heightSize
    elif border == 4:
        brick.yPos = 0